package com.ttg.web.config;

import com.ttg.common.entities.BaseResult;
import com.ttg.common.enums.ErrorCodeEnums;
import com.ttg.common.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author zhangHongYuan
 * @description
 * @date 2023/7/7 14:10
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = BaseException.class)
    public BaseResult handleBaseException(BaseException e) {
        log.error("--统一异常处理--BaseException--", e);
        //BaseResult result = new BaseResult(e.getErrorCode(), e.getMsg());
        BaseResult baseResult = new BaseResult();
        baseResult.setErrcode(e.getErrcode());
        baseResult.setMsg(e.getMsg());
        return baseResult;
    }

    @ExceptionHandler(value = Exception.class)
    public BaseResult handler(Exception e) {
        log.error("--统一异常处理--", e);
        BaseResult baseResult = new BaseResult();
        baseResult.setErrcode(ErrorCodeEnums.SYSTEM_ERROR.getErrcode());
        baseResult.setMsg(ErrorCodeEnums.SYSTEM_ERROR.getMsg());
        return baseResult;
    }
}
